<template>
  <div class="main">
    <div class="content">
      <div class="search_bar">
        <div class="input_wrap">
          <input class="input_text" type="text" placeholder="请输入商品名" v-model="search_word">
        </div>
        <div class="button" @click="search">
          <span class="text">搜索</span>
        </div>
      </div>
      <div class="search_content">
        <div class="line">
          <div class="name">商品名</div>
          <div class="price">价格</div>
          <div class="stock">剩余数量</div>
          <div class="function">功能</div>
        </div>
        <div class="content">
          <div class="line" v-for="item in items" :key="item.barCode">
            <div class="name">{{item.commodity}}</div>
            <div class="price">{{item.price}}</div>
            <div class="stock">{{item.surplus}}</div>
            <div class="function">
              <div class="function_button" @click="down(item.barCode)" >下架</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import shopOwnerService from '../../service/shopowner'
export default {
  name: 'ShelfDown',
  data () {
    return {
      search_word: '',
      items: null
    }
  },
  mounted () {
  },
  methods: {
    async search () {
      // 如果当前用户已登录，从用户那里获取本店id，如果未登录，不允许提交
      const loginedUser = this.$store.getters.getLoginUser
      if (loginedUser == null) {
        return
      }
      let result = await shopOwnerService.searchGoodsByStoreIdAndCommodity(loginedUser.storeId, this.search_word)
      this.items = result.data
      // 设置价格
      for (let i = 0; i < this.items.length; i++) {
        if (this.items[i].useSpecialPrice) {
          this.items[i].price = this.items[i].specialPrice
        } else {
          this.items[i].price = this.items[i].originalPrice
        }
      }
    },
    async down (barcode) {
      // 如果当前用户已登录，从用户那里获取本店id，如果未登录，不允许提交
      const loginedUser = this.$store.getters.getLoginUser
      if (loginedUser == null) {
        return
      }
      let result = await shopOwnerService.shelfDownGoods(loginedUser.storeId, barcode)
      if (result.data > 0) {
        alert('下架成功')
        await this.search()
      } else {
        alert('下架失败')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .main{
    width: 100%;
    height: calc(100% - 1px);
    background: #FFFFFF;
    border-top: 1px solid transparent;
    .content {
      width: 1000px;
      margin: 0 auto;
      /*border: 1px solid black;*/
      position: relative;
      top: 0;
      left: 0;
      .search_bar {
        width: 670px;
        height: 50px;
        position: relative;
        margin: 100px auto 0;
        /*border: 2px solid black;*/
        .input_wrap{
          width: 550px;
          height: 50px;
          .input_text{
            width: 550px;
            height: 50px;
            text-align: center;
            font-size: 20px;
            line-height: 50px;
            border-radius: 5px;
          }
        }
        .button{
          width: 60px;
          height: 30px;
          border: 1px solid black;
          border-radius: 5px;
          position: absolute;
          top: 10px;
          left: 600px;
          text-align: center;
          line-height: 30px;
          .text {
            font-size: 14px;
          }
        }
      }
      .search_content {
        width: 700px;
        margin: 30px auto 0;
        .line {
          height: 30px;
          position: relative;
          border: 2px solid black;
          overflow: hidden;
          text-align: center;
          @mixin col {
            position: absolute;
            top: 0;
            border-right: 1px solid black;
            height: 30px;
            line-height: 30px;
            padding: 0 0 0 5px;
            overflow: hidden;
          }
          .name{
            @include col;
            left: 0;
            width: 195px;
          }
          .price{
            @include col;
            left: 200px;
            width: 195px;
          }
          .stock{
            @include col;
            left: 400px;
            width: 195px;
          }
          .function {
            @include col;
            left: 600px;
            width: 95px;
            .function_button {
              width: 30px;
              height: 20px;
              margin: 5px 5px 0;
              text-align: center;
              line-height: 20px;
              border: 1px solid black;
              border-radius: 2px;
            }
          }
        }
        .content{
          width: 700px;
          height: 400px;
          /*border: 2px solid blue;*/
          overflow: auto;
        }

      }

    }
  }

</style>
